<template>
  <div>
    <el-upload
      class="upload"
      v-show="!isShowProgress"
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :class="{ 'display': fileList.length}"
      :on-remove="onRemove"
      :http-request="onRequest"
      :on-change="onChange"
      :on-preview="handlePictureCardPreview"
      :before-upload="onBeforeUpload"
    >
      <span v-if="!fileList.length" class="closeUpload el-icon-circle-close"></span>
      <span>上传图片</span>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-upload>
<!--    <el-progress v-if="isShowProgress" type="circle" :percentage="percentage"></el-progress>-->
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDkIcgTzEExRhjUyB7nCLDpAE1Yopb7NJy',
  SecretKey: 'PYQ3QWLtAAuNaXEeohKgvwhCQN3SaliP'
})
// id: AKIDkIcgTzEExRhjUyB7nCLDpAE1Yopb7NJy
// key: PYQ3QWLtAAuNaXEeohKgvwhCQN3SaliP
export default {
  data () {
    return {
      fileList: [],
      dialogVisible: false, // 对话框显示
      dialogImageUrl: '', // 对话框预览图片地址
      currectId: '',
      percentage: 0,
      isShowProgress: false
    }
  },
  props: {
    url: {
      type: String,
      required: true
    }
  },
  mounted () {
    console.log()
    if (this.url) {
      this.fileList.push({
        url: this.url
      })
    }
  },
  watch: {
  },
  methods: {
    // 移除图片时触发
    onRemove (file, fileList) {
      this.fileList = fileList
    },
    // 手动发请求
    onRequest (data) {
      cos.putObject({
        Bucket: 'hmmm-1307760730', /* 必须 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: data.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file // 上传文件对象
        // onProgress: (progressData) => {
        //   this.percentage = progressData.percent * 100
        // }
      }, (err, data) => {
        if (!err && data.statusCode === 200) {
          const url = 'https://' + data.Location
          this.fileList = this.fileList.map(item => {
            if (item.uid === this.currectId) {
              item.url = url
            }
            return item
          })
          setTimeout(() => {
            this.isShowProgress = false
          }, 700)
          this.$emit('on-success', this.fileList)
        } else {
          this.$message.error('上传失败,重新上传')
          this.fileList = this.fileList.filter(item => item.uid !== this.currectId)
          this.$nextTick(() => {
            this.isShowProgress = false
          })
        }
      })
    },
    // 本地上传成功
    onChange (file, fileList) {
      this.fileList = fileList
    },
    // 点击图片预览
    handlePictureCardPreview (file) {
      this.dialogVisible = true
      this.dialogImageUrl = file.url
    },
    // 文件上传之前的钩子
    onBeforeUpload (file) {
      const type = ['image/gif', 'image/png', 'image/jpeg']
      const imageMaxSize = 1 * 1024 * 1024
      if (!type.includes(file.type)) {
        this.$message.error('图片格式不正确')
        return false
      }
      if (file.size > imageMaxSize) {
        this.$message.error('图片不能大于2Mb')
        return false
      }
      this.currectId = file.uid
      this.isShowProgress = true
      return true
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .display .el-upload--picture-card {
  display: none !important;

}

  /deep/ .el-upload-list__item {
    width: 100px !important;
    vertical-align: middle !important;
    background-color: #fff !important;
    border: none !important;
    line-height: 60px;
    height: 60px !important;
  }

  /deep/ .el-upload--picture-card {
    width: 100px !important;
    vertical-align: middle !important;
    background-color: #fff !important;
    line-height: 60px;
    height: 60px !important;
    display: inline-block;
    position: relative;
    border: 1px dashed #d9d9d9;
  }

  /deep/ .el-upload-input {
    width: 100px;
    height: 60px;
    z-index: 100;
  }

  ///deep/ .upLoadBtn {
  //  display: inline-block;
  //  position: relative;
  //  width: 100px;
  //  height: 60px;
  //  border: 1px dashed #d9d9d9;
  //}

  /deep/ .upload {
    display: inline-block;
    position: relative;
    margin-left: 5px;
  }

  /deep/ .closeUpload {
    position: absolute;
    right: -1px;
    top: -7px;
    display: block;
    width: 10px;
    height: 10px;
    font-size: 16px;
    border-radius: 50%;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
    color: #999;
  }

  /deep/ .el-upload--picture-card:hover {
    border: 1px dashed #6b8dfc;
  }

  /deep/ .el-upload-list__item-status-label {
    display: none;
  }
</style>
